package edu.dayu.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component
public class CreateAndUpdateEntityHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasGetter("createtime")){
            if (metaObject.getValue("createtime")==null){
                this.setFieldValByName("createtime",LocalDateTime.now().toString(),metaObject);
            }
        }
        if (metaObject.hasGetter("updatetime")){
            if (metaObject.getValue("updatetime")==null){
                this.setFieldValByName("updatetime", LocalDateTime.now().toString(),metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasGetter("updatetime")){
            if (metaObject.getValue("updatetime")==null){
                this.setFieldValByName("updatetime",LocalDateTime.now().toString(),metaObject);
            }
        }
    }
}
